<?php

namespace App\Repository\Orders;

use App\Jobs\OrderSubmit;
use App\Jobs\TaskUploadExcel;
use App\Models\Address\CitiesModel;
use App\Models\Address\ProvincesModel;
use App\Models\Orders\OrdersModel;
use App\Models\Orders\OrdersModel as model;
use App\Models\Orders\OrdersRemarkModel;
use App\Models\Orders\OrdersUserModel;
use App\Models\Product\GoodsModel;
use App\Models\Product\ProductModel;
use App\Models\Product\SetAddressModel;
use App\Repository\Api\GoodsRepository as apiGoods;
use App\Repository\BaseRepository;

//定单处理
class HandlesRepository extends BaseRepository
{
    public $model;
    public $ModelOrdersUser;

    public function __construct()
    {
        $this->model = new model();
        $this->ModelOrdersUser = new OrdersUserModel();
        parent::__construct();
    }

    public function parseNumberRemark($str)
    {
        $data = ['number_min_long_time' => '', 'number_month_consume' => ''];
        if (empty($str)) {
            return $data;
        }
        preg_match('/在网时长([0-9]+)个月/', $str, $tmp);
        if (isset($tmp[1])) {
            $data['number_min_long_time'] = $tmp[1];
        }
        preg_match('/承诺通信费([0-9]+)元/', $str, $tmp);
        if (isset($tmp[1])) {
            $data['number_month_consume'] = $tmp[1];
        }
        return $data;
    }

//选号
    public function getSelectNumberTwo($data)
    {
        $goodsId = $data['goodsId'];
        $endNumber = $data['endNumber'] ?? '';
        $symbol = $data['symbol'] ?? '';
        $amounts = $data['amounts'] ?? 30;
        $province = $data['province'] ?? '';
        $city = $data['city'] ?? '';

        $ModelGoods = new GoodsModel();
//        根据goodsId查出相应的地区信息
        $where = [];
        $where['where']['id'] = $goodsId;
        $fields = ['product_id', 'product_code'];
        $row = $ModelGoods->select($fields)->searchWhere($where)->first();
        if (empty($row)) {
            return $this->returnMsg(2, '找不到产品的数据');
        }
        $productId = $row['product_id'];

        $apiParams = [
            'id' => $goodsId,
            'codeTypeCode' => $symbol,
            'searchValue' => $endNumber,
            'product_id' => $productId,
            'amounts' => $amounts
        ];
        if (!empty($province)) {
            $numberProvince = str_replace('省', '', $province);
            $apiParams['numberProvince'] = $numberProvince;
        }
        if (!empty($city)) {
            $numbercity = str_replace('县', '', $city);
            if (empty($numbercity)) {
                $numbercity = $numberProvince;
            }
            $apiParams['numbercity'] = $numbercity;
        }
        $apiGoods = new apiGoods();
        $data = $apiGoods->getSelectNumberTwo($productId, $apiParams);
        if (!empty($data['data'])) {
            // foreach ($data['data'] as $k => $v) {
            //     $v['number_min_long_time'] = '';
            //     $v['number_month_consume'] = '';
            //     if (isset($v['number_remark'])) {
            //         $parseResult = $this->parseNumberRemark($v['number_remark']);
            //         $v['number_min_long_time'] = $parseResult['number_min_long_time'];
            //         $v['number_month_consume'] = $parseResult['number_month_consume'];
            //     }
            //     $data['data'][$k] = $v;
            // }
            $numberProvince = $data['data'][0]['province'] ?? '';
            $numbercity = $data['data'][0]['city'] ?? '';
            $data['data'] = array_slice($data['data'], 0, $amounts);
        }
        $data['province'] = $numberProvince;
        $data['city'] = $numbercity;
        return $data;
    }

//这个可以不用了,现 在 用了getSelectNumberTwo
    public function getSelectNumber($data)
    {
        $goodsId = $data['goodsId'];
        $endNumber = $data['endNumber'] ?? '';
        $amounts = $data['amounts'] ?? 30;
        $province = $data['province'] ?? '';
        $city = $data['city'] ?? '';

        $ModelGoods = new GoodsModel();
        $ModelProduct = new ProductModel();
        $ModelProductAddress = new SetAddressModel();
        $param = [
            'searchValue' => $endNumber,
            'amounts' => $amounts
        ];
//        根据goodsId查出相应的地区信息
        $where = [];
        $where['where']['a.id'] = $goodsId;
        $fields = ['a.id', 'a.product_id', 'a.product_code', 'b.product_address'];
        $row = $this->model->from($ModelGoods->getTable(), 'a')
            ->leftJoin($ModelProduct->getTable() . ' AS b', 'a.product_id', '=', 'b.id');
        if (empty($province) && empty($city)) {
            $fields[] = 'c.owner';
            $row = $row->leftJoin($ModelProductAddress->getTable() . ' AS c', 'b.product_address', '=', 'c.id')
                ->select($fields)
                ->searchWhere($where)->first();
            if (empty($row)) {
                return $this->returnMsg(1, '找不到相应的数据', []);
            }
            $provinceCityArray = [];
            if (!empty($row['owner'])) {
                $row['owner'] = json_decode($row['owner'], true);
                if (is_array($row['owner'])) {
//                随机取一个地址
                    if (count($row['owner']) > 1) {
                        $randKey = array_rand($row['owner'], 1);
                        $provinceCityArray = $row['owner'][$randKey];
                    } else {
                        $provinceCityArray = $row['owner'][0];
                    }
                }
            }
            if (empty($provinceCityArray)) {
                return $this->returnMsg(2, '区域数据异常');
            }

//        不能传省市代码,因为这里获取到的代号与接口的代码不一样,传省市名给那边再查一下
            $ModelProvince = new ProvincesModel();
            $province = $ModelProvince->where(['id' => $provinceCityArray[0]])->value('name');
            $ModelCity = new CitiesModel();
            $city = $ModelCity->where(['id' => $provinceCityArray[1]])->value('name');
        } else {
//            已经有省市,不直接跳过查模板
            $row = $row->select($fields)
                ->searchWhere($where)->first();
        }
        if (empty($row)) {
            return $this->returnMsg(2, '找不到产品的数据');
        }
        $productId = $row['product_id'];
        $param['numberProvince'] = str_replace('省', '', $province);
        $param['numbercity'] = str_replace('县', '', $city);
        if (empty($param['numbercity'])) {
            $param['numbercity'] = $param['numberProvince'];
        }
//        print_r($param);
//        $GoodsRepository = new GoodsRepository();
//        $SetAddressRepository = new SetAddressRepository(new SetAddressModel());
        $apiGoods = new apiGoods();
//        $Api = new \App\Http\Controllers\Api\GoodsController($GoodsRepository, $SetAddressRepository, $apiGoods);
        $data = $apiGoods->getSelectNumber($productId, $param);
        // if (!empty($data['data'])) {
        //     foreach ($data['data'] as $k => $v) {
        //         $v['number_min_long_time'] = '';
        //         $v['number_month_consume'] = '';
        //         if (isset($v['number_remark'])) {
        //             $parseResult = $this->parseNumberRemark($v['number_remark']);
        //             $v['number_min_long_time'] = $parseResult['number_min_long_time'];
        //             $v['number_month_consume'] = $parseResult['number_month_consume'];
        //         }
        //         $data['data'][$k] = $v;
        //     }
        //     $data['data'] = array_slice($data['data'], 0, $amounts);
        // }
        $data['province'] = $param['numberProvince'];
        $data['city'] = $param['numbercity'];
        return $data;
    }

//修改订单信息
    public function changeInfo($params)
    {
        $ordersId = (int)$params['id'];
        $userId = $params['user']['id'];
        $oldRow = $this->model->where(['id' => $ordersId])->first();
        $this->changeGoodsInfo($params, $oldRow);
        $this->changeBuyerInfo($params);
        $this->changeStatus($params);
//        修改物流
        $this->changeExpress($params, $oldRow);
        if (isset($params['iccid']) && $params['iccid'] != $oldRow['iccid']) {
            $logMsg = '修改生产号码为:' . $params['iccid'];
            $upData = ['iccid' => $params['iccid']];
            if ($this->model->where(['id' => $ordersId])->update($upData)) {
                $LogsModel = new \App\Models\LogsModel();
                $LogsModel->saveIt(0, $userId, $ordersId, $logMsg);
            }
        }
        return $this->returnMsg(200, '修改操作完成');
    }

    //        修改物流
    public function changeExpress($params, $oldRow = [])
    {
        $ordersId = (int)$params['id'];;
        $userId = $params['user']['id'];

//        本次数据操作只允许的字段
        $mustFields = [
            'id' => '订单id必填写',
            'express_company' => '快递公司必填写',
            'express_number' => '快递单号必填写'
        ];
        $upData = $this->getRealData($params, array_keys($mustFields), [], $mustFields);
        if (!empty($upData) && is_string($upData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $upData
            );
        }
        if (empty($oldRow)) {
            $oldRow = $this->model->select(['express_number', 'express_company'])->where(['id' => $ordersId])->first();
        }
//        $ordersno = $oldRow['ordersno'];
        if ($oldRow['express_number'] == $upData['express_number']) {
            return $this->returnMsg(10, '快递未变化,无需修改');
        }
        $logMsg = '修改快递为:' . $upData['express_company'] . ',单号为:' . $upData['express_number'];
        unset($upData['id']);
        if ($this->model->where(['id' => $ordersId])->update($upData)) {
//            提交要物流信息
            dispatch(new TaskUploadExcel('RequestLogistics', 0, $upData))->onQueue('TaskUploadExcelDispose');
//            提交上游重新发货
            dispatch(new OrderSubmit($oldRow, 'smsAndPushOrder', 60))->onQueue('TaskOrderSubmitDispose');
            //           记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $LogsModel->saveIt(0, $userId, $ordersId, $logMsg);
        }
        return $this->returnMsg(200, '操作成功');
    }

//    修改商品信息
    public function changeGoodsInfo($data, $oldRow = [])
    {
        $ordersId = $data['id'];
        $dealUserId = $data['user']['id'];
//        $this->log->info('用户信息修改查询条件:' . json_encode($where) . '修改参数:' . json_encode($params));
        $postMustFieldsMsg = [
            'id' => '请传递id参数',
            'goods_id' => '请传递商品id参数',
        ];
        $result = $this->getRealData($data, array_keys($postMustFieldsMsg), [], $postMustFieldsMsg);
        if (!empty($result) && is_string($result)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $result
            );
        }
        if (empty($oldRow)) {
            $isTrue = $this->model->where(['id' => $ordersId])->first();
        } else {
            $isTrue = $oldRow;
        }
        if ($isTrue['goods_id'] == $data['goods_id']) {
            return $this->returnMsg(10, '商品未变化,无需修改');
        }
        $logMsg = [];
        $field = ['goods_id' => $data['goods_id']];

        // 商品得上架
        $ModelGoods = new GoodsModel();
        $resGoods = $ModelGoods->where(['id' => $field['goods_id']])->first();
        if (empty($resGoods)) {
            return $this->returnMsg(1, '未查询到商品');
        }
        if ($resGoods->status != 0) {
            return $this->returnMsg(1, '商品已下架,请上架商品后在提交');
        }

        $logMsg[] = '修改商品信息为:id:' . $data['goods_id'] . ',名称:' . $resGoods->name;
        $field['goods_code'] = $resGoods->goods_code;
        $field['goods_name'] = $resGoods->name;

        $field['product_id'] = $resGoods->product_id;
        $field['product_name'] = $resGoods->product_name;
        $field['product_code'] = $resGoods->product_code;
        $field['sup_shop_name'] = $resGoods->sup_shop_name;
        $field['shop_name_str'] = $resGoods->shop_name_str;
        $field['sup_goods_name'] = $resGoods->sup_goods_name;
        $field['shop_id_str'] = $resGoods->shop_id_str;
        $field['sup_goods_id_str'] = $resGoods->sup_goods_id_str;
        $field['sup_shop_id'] = $resGoods->sup_shop_id;
        $field['shop_name'] = $resGoods->shop_name;
        $field['shop_str'] = $resGoods->shop_str;
//        这些参考原程序,也不知道有什么意思OrdersRepository.edit

        $field['sup_goods_id'] = $isTrue['goods_id'];
//        $field['sup_goods_name'] = $isTrue['goods_name'];
        $field['sup_goods_code'] = $isTrue['goods_code'];
        $field['sup_product_code'] = $isTrue['product_code'];
        $field['sup_product_name'] = $isTrue['product_name'];
        $field['sup_product_id'] = $isTrue['product_id'];
        $field['one_product_code'] = $isTrue['one_product_code'];
        $field['one_product_name'] = $isTrue['one_product_name'];
        $field['one_product_id'] = $isTrue['one_product_id'];
        // 关联到产品信息
        if (!empty($resGoods->sup_goods_id_str)) {
            $resGoodsArr = explode('.', $resGoods->sup_goods_id_str);
            if (!empty($resGoodsArr[1])) {
                $resGoodsProduct = $ModelGoods->where(['id' => $resGoodsArr[1]])->first();
                $field['product_id'] = $resGoodsProduct->product_id;
                $field['product_name'] = $resGoodsProduct->product_name;
                $field['product_code'] = $resGoodsProduct->product_code;
            }
        }
        $res = $this->model->where(['id' => $ordersId])->update($field);
        if (false !== $res) {
            //            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $LogsModel->saveIt(0, $dealUserId, $ordersId, implode(';', $logMsg));
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(1, '修改异常');
    }

//    修改购买者信息
    public function changeBuyerInfo($data)
    {
        $ordersId = $data['id'];
        $dealUserId = $data['user']['id'];
//        $this->log->info('用户信息修改查询条件:' . json_encode($where) . '修改参数:' . json_encode($params));
        $postMustFieldsMsg = [
            'id' => '请传递id参数'
        ];
        $result = $this->getRealData($data, array_keys($postMustFieldsMsg), [], $postMustFieldsMsg);
        if (!empty($result) && is_string($result)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $result
            );
        }
//        本次只能修改的字段
        $optionalFields = [
            'pay_name' => '用户名称',
            'pay_phone' => '用户联系方式',
            'pay_identity' => '身份信息',
            'post_province' => '用户地址省',
            'post_city' => '用户地址市',
            'post_area' => '用户地址区',
            'post_address' => '用户详细地址'
        ];
        $postData = $this->getRealData($data, [], array_keys($optionalFields), []);
        if (empty($postData) || is_string($postData)) {
            return $this->returnMsg(
                2,
                $postData
            );
        }
        $OrdersTable = $this->model->getTable();
        $OrdersUserTable = $this->ModelOrdersUser->getTable();
        //        查询条件
        $where = [];
        $where['where']['a.id'] = $data['id'];
        $where['where']['b.shop_id'] = $data['user']['shop_id'];
        $fields = ['a.id', 'a.ordersno'];
        foreach ($postData as $field => $value) {
            $fields[] = 'b.' . $field;
        }
        $oldRow = $this->model->from($OrdersTable, 'a')
            ->leftJoin($OrdersUserTable . ' AS b', 'b.id', '=', 'a.orders_user_id')
            ->select($fields)
            ->searchWhere($where)->first();
        if (empty($oldRow)) {
            return $this->returnMsg(3, '您无权限修改订单信息');
        }
        $logMsg = [];
        $upData = [];
        if (!empty($postData['pay_name']) && $oldRow['pay_name'] != $postData['pay_name']) {
            $logMsg[] = '付款人名字:' . $postData['pay_name'];
            $upData['pay_name'] = $postData['pay_name'];
        }
        if (!empty($postData['pay_phone']) && $oldRow['pay_phone'] != $postData['pay_phone']) {
            $logMsg[] = '付款人手机:' . $postData['pay_phone'];
            $upData['pay_phone'] = $postData['pay_phone'];
        }
        if (!empty($postData['pay_identity']) && $oldRow['pay_identity'] != $postData['pay_identity']) {
            $logMsg[] = '身份信息:' . $postData['pay_identity'];
            $upData['pay_identity'] = $postData['pay_identity'];
        }
        if (!empty($postData['post_province']) && str_replace(['省', '自治区'], '', $oldRow['post_province']) != str_replace(['省', '自治区'], '', $postData['post_province'])) {
            $logMsg[] = '用户省:' . $postData['post_province'];
            $upData['post_province'] = $postData['post_province'];
        }
        if (!empty($postData['post_city']) && str_replace(['市', '区', '县'], '', $oldRow['post_city']) != str_replace(['市', '区', '县'], '', $postData['post_city'])) {
            $logMsg[] = '用户市:' . $postData['post_city'];
            $upData['post_city'] = $postData['post_city'];
        }
        if (!empty($postData['post_area']) && $oldRow['post_area'] != $postData['post_area']) {
            $logMsg[] = '用户区:' . $postData['post_area'];
            $upData['post_area'] = $postData['post_area'];
        }
        if (!empty($postData['post_address']) && $oldRow['post_address'] != $postData['post_address']) {
            $logMsg[] = '用户详细地址:' . $postData['post_address'];
            $upData['post_address'] = $postData['post_address'];
        }
        if (!empty($upData)) {
            $res = $this->ModelOrdersUser->where(['orderno' => $oldRow['ordersno']])->update($upData);
            if (false !== $res) {
                //            记录操作日志
                $logMsg = '修改了用户信息:' . implode(';', $logMsg);
                $LogsModel = new \App\Models\LogsModel();
                $LogsModel->saveIt(0, $dealUserId, $ordersId, $logMsg);
                return $this->returnMsg(200, '修改成功');
            }
        }
        return $this->returnMsg(1, '无数据修改');
    }

    //审核首充与激活
    public function reviewRecharge($postData)
    {
        $ordersId = (int)$postData['id'];
        $userId = $postData['user']['id'];
//        本次数据操作只允许的字段
        $fields = ['remark', 'activat_at', 'activat_status', 'first_status', 'first_status_at', 'first_money'];
        $upData = $this->getRealData($postData, ['id'], $fields);
        if (!empty($upData) && is_string($upData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $upData
            );
        }
        $oldData = $this->model->where(['id' => $ordersId])->select(['data_review_status', 'remark'])->first();
        $LogActivatMsg = $LogFirstMsg = '';
        if (isset($postData['activat_status'])) {
            $upData['review_activat_user_id'] = $userId;
            $LogActivatMsg = '激活状态为:' . config('card.reviewStatus')[$postData['activat_status']];
        }
        if (isset($postData['first_status'])) {
            $upData['review_first_user_id'] = $postData['user']['id'];
            $LogFirstMsg = '首充状态为:' . config('card.reviewStatus')[$postData['first_status']];
        }
//        客服提交了,给到审核员审核了
        if ($oldData['data_review_status'] == 0) {
            $upData['data_review_status'] = 1;
        }
        if (false !== $this->model->where(['id' => $ordersId])->update($upData)) {
            if (!empty($upData['remark']) && $oldData['remark'] !== $upData['remark']) {
                $ModelRemark = new OrdersRemarkModel();
                $ModelRemark->saveIt($userId, $ordersId, $upData['remark']);
            }
//            记录操作日志
            $LogsModel = new \App\Models\LogsModel();

            $LogsModel->saveIt(0, $userId, $ordersId, "审核了订单，状态为[" . $LogActivatMsg . $LogFirstMsg . "]");

        }
        return $this->returnMsg(200, '操作成功');
    }

    public function dataReview($params)
    {
        $ordersId = (int)$params['ordersId'];
        $userId = $params['user']['id'];
//        本次数据操作只允许的字段
        $fields = ['remark', 'status'];
        $upData = $this->getRealData($params, ['ordersId'], $fields);
        if (!empty($upData) && is_string($upData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $upData
            );
        }
        $oldStatus = $this->model->where(['id' => $ordersId])->value('data_review_status');
        if ($oldStatus != 1) {
            return $this->returnMsg(10, '数据状态为非等待审核状态');
        }
        $logMsg = '数据审核状态为:' . config('card.dataReviewStatus')[$upData['status']];
        if (isset($upData['remark'])) {
            $logMsg .= ';备注:' . $upData['remark'];
        }
        $ordersUpdateData = ['data_review_user_id' => $userId, 'data_review_status' => intval($upData['status'])];
        if (false !== $this->model->where(['id' => $ordersId])->update($ordersUpdateData)) {
//            备注在orders_user表
            if (!empty($upData['remark'])) {
                $ModelOrdersUser = new OrdersUserModel();
                $ModelOrdersUser->where(['orders_id' => $ordersId])->update(['remark' => $upData['remark']]);
            }
//            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $LogsModel->saveIt(0, $userId, $ordersId, $logMsg);
        }
        return $this->returnMsg(200, '操作成功');
    }

//拉黑
    public function blocking($postData)
    {
        $dealUserId = $postData['user']['id'];
        $where = [];
        if ($postData['user']['shop_id'] != SUPPER_SHOP_ID) {
            $where['eq'] ['shop_id'] = $postData['user']['shop_id'];
        }
        $id = $postData['id'];
        $logs = [];
        if (is_array($id)) {
            $where['in']['id'] = $id;
            foreach ($id as $k => $v) {
                $logs[] = [
                    'type' => 0,
                    'table_id' => $v,
                    'users_id' => $dealUserId,
                    'created_at' => date('Y-m-d H:i:s'),
                    'content' => '拉黑了订单'
                ];
            }
        } else {
            $where['eq']['id'] = $id;
            $logs = [
                'type' => 0,
                'table_id' => $id,
                'users_id' => $dealUserId,
                'created_at' => date('Y-m-d H:i:s'),
                'content' => '拉黑了订单'
            ];
        }
        $ModelOrders = new OrdersModel();
        $res = $ModelOrders->searchWhere($where)->update(['blocking' => 1]);
        if (false !== $res) {
            //            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $LogsModel->insert($logs);
        }
        return $this->returnMsg(200, '拉黑操作成功');
    }

//修改状态
    public function changeStatus($params)
    {
        $ordersId = $params['id'];
        $dealUserId = $params['user']['id'];
        $postMustFieldsMsg = [
            'id' => '请传递id参数',
            'status' => '请传递status参数'
        ];
        $postData = $this->getRealData($params, array_keys($postMustFieldsMsg), [], $postMustFieldsMsg);
        if (!empty($postData) && is_string($postData)) {
            //返回错误参数
            return $this->returnMsg(
                2,
                $postData
            );
        }
        $OrdersTable = $this->model->getTable();
        $OrdersUserTable = $this->ModelOrdersUser->getTable();
        //        查询条件
        $where = [];
        $where['where']['a.id'] = $params['id'];
        $where['where']['b.shop_id'] = $params['user']['shop_id'];
        $row = $this->model->from($OrdersTable, 'a')
            ->leftJoin($OrdersUserTable . ' AS b', 'b.id', '=', 'a.orders_user_id')
            ->searchWhere($where)->select(['a.ordersno', 'a.status'])->first();
//        echo $this->getLastSql();
        if (empty($row)) {
            return $this->returnMsg(1, '您无权限修改订单信息');
        }
        if ($row['status'] == $postData['status']) {
            return $this->returnMsg(10, '订单状态未改变');
        }
        $res = $this->model->where(['id' => $ordersId])->update(['status' => $postData['status']]);
        if (false !== $res) {
            // 推送消息发短信
//            dispatch(new OrderSubmit([
//                'ordersno' => $row->ordersno,
//            ], 'smsAndPushOrder', 60))->onQueue('TaskOrderSubmitDispose');
            //            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $logMsg = "修改订单状态为:" . $this->model->getOrderStatusDesc($postData['status']);
            $LogsModel->saveIt(0, $dealUserId, $ordersId, $logMsg);
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(1, '修改异常');

    }

//    修改订单的指定字段信息,对那些不用提交上游的信息的修改
    public function changeOrdersFields($dealUserId, $ordersId, $fieldValue = [])
    {
        $logsMsg = [];
        foreach ($fieldValue as $field => $value) {
            switch ($field) {
                case 'forbid_status':
                    $logsMsg[] = '禁止了导入时修改订单状态';
                    break;
                case 'forbid_activat_status':
                    $logsMsg[] = '禁止了导入时修改订单状态';
                    break;
                case 'forbid_first_status':
                    $logsMsg[] = '禁止了导入时修改订单状态';
                    break;
                case 'forbid_express_status':
                    $logsMsg[] = '禁止了导入时修改订单状态';
                    break;
                case 'status':
                    $logsMsg[] = '修改订单状态为:' . $this->model->getOrderStatusDesc($value);
                    break;
                case 'blocking':
                    $logsMsg[] = '拉黑了此订单';
                    break;
            }
        }
        if (empty($fieldValue)) {
            return $this->returnMsg(1, '数据异常');
        }
        $res = $this->model->where(['id' => $ordersId])->update($fieldValue);
        if (false !== $res) {
            //            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $logMsg = implode(';', $logsMsg);
            $LogsModel->saveIt(0, $dealUserId, $ordersId, $logMsg);
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(1, '修改异常');

    }

//    修改备注
    public function changeRemark($params)
    {
        if (empty($params['id'])) {
            return $this->returnMsg(100, '数据异常');
        }
        $dealUserId = $params['user']['id'];
        $where = [
            'id' => $params['id'],
            'shop_id' => $params['user']['shop_id']
        ];
        $ordersId = $params['id'];
        $mustFieldsMsg = ['remark' => '备注不能为空'];
        $field = $this->getRealData($params, array_keys($mustFieldsMsg), [], $mustFieldsMsg);
        $res = $this->model->where($where)->update($field);
        if (false !== $res) {
            $ModelRemark = new OrdersRemarkModel();
            $type = $params['type'] ?? 0;
            $ModelRemark->saveIt($dealUserId, $ordersId, $field['remark'], $type);
            //            记录操作日志
            $LogsModel = new \App\Models\LogsModel();
            $logMsg = "更新了订单备注:" . $field['remark'];
            $LogsModel->saveIt(0, $dealUserId, $ordersId, $logMsg);
            return $this->returnMsg(200, '修改成功');
        }
        return $this->returnMsg(1, '修改异常');
    }

    public function editSkuArea($params)
    {
        if (!isset($params['orderno']) || empty($params['orderno'])) {
            return $this->returnMsg(1, '参数异常');
        }
        $where = [
            'orderno' => $params['orderno']
        ];
        if (empty($params['area'])) {
            return $this->returnMsg(2, 'area值异常');
        }
        $data = [
            'sku_area' => $params['area']
        ];
        $this->ModelOrdersUser->where($where)->update($data);
        return $this->returnMsg(200, '记录完成');
    }

}
